package com.greenline.push.home.controllers.classuser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.greenline.push.common.json.BaseJson;
import com.greenline.push.core.pushcenter.ClassBatchDefineManager;
import com.greenline.push.core.pushcenter.ClassDefineManager;
import com.greenline.push.core.pushcenter.ClassUserManager;
import com.greenline.push.dal.pushcenter.dataobject.ClassBatchDefineDO;
import com.greenline.push.dal.pushcenter.dataobject.ClassDefineDO;

@Controller
@RequestMapping(value = "/classuser")
public class ClassUserController {

	@Autowired
	private ClassBatchDefineManager batchManager;

	@Autowired
	private ClassUserManager userManager;

	@Autowired
	private ClassDefineManager classManager;
	private final Log logger = LogFactory.getLog(ClassUserController.class);

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public ModelAndView upload(MultipartHttpServletRequest request,
			Long id) {
		ModelAndView view = new ModelAndView("/class/upload_add");
		ClassDefineDO classDefineDO = classManager.getClassDefineById(id);
		BaseJson json = new BaseJson();

		MultipartFile file = request.getFile("file");
		InputStream in = null;
		String fileName = "";
		List<Long> uidList = new ArrayList<Long>();
		try {
			in = file.getInputStream();
			 fileName = file.getOriginalFilename();
			InputStreamReader read = new InputStreamReader(in);
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				Long uid = Long.parseLong(lineTxt);
				uidList.add(uid);
			}
			read.close();
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			view.addObject("classdefine", classDefineDO);
			view.addObject("issucc", false);
			view.addObject("result", "文件解析错误！");
			json.setHasError(true);
			json.setMessage("文件解析错误！");
			return view;
		}catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			view.addObject("classdefine", classDefineDO);
			view.addObject("issucc", false);
			view.addObject("result", "文件解析错误！");
			json.setHasError(true);
			json.setMessage("文件解析错误！");
			return view;
		}
		
		int successNum = userManager.insertUidList(uidList, id);
		json.setMessage("上传成功！");
		view.addObject("classdefine", classDefineDO);
		view.addObject("issucc", true);
		view.addObject("result", "插入成功");
		view.addObject("total", successNum);
		
		ClassBatchDefineDO batchDO = new ClassBatchDefineDO();
		batchDO.setClassId(id);
		batchDO.setClassName(classDefineDO.getName());
		batchDO.setFileName(fileName);
		batchDO.setSuccessNum((long) successNum);
		batchDO.setTotalNum((long) uidList.size());
		batchManager.insert(batchDO );
		
		return view;
	}

}
